package com.sc.web.goods;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.ParamMap;
import com.sc.dal.model.goods.GoodsBrand;
import com.sc.dal.model.system.UserFormMap;
import com.sc.service.goods.GoodsBrandService;
import com.sc.util.data.DataUtil;
import com.sc.util.data.DateUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;
import com.sc.web.BaseController;
import com.sc.web.plugin.PageView;

/**
 * 商品品牌管理
 * @author maoyongbin
 * @日期 Mar 27, 2017 3:46:08 PM
 */
@Controller
@RequestMapping(value="goods/goodsBrand")
public class GoodsBrandController extends BaseController{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private GoodsBrandService goodsBrandService;
	@Autowired
	private HttpSession session;
	
	
	/**
	 * 跳转到商品品牌列表页面
	 * @param request
	 * @return String
	 */
	@RequestMapping(value="list")
	public String list(HttpServletRequest request){
		return "goods/goodsBrand/list";
	}
	
	/**
	 * 查找所有的商品品牌
	 * @return List<GoodsBrandController>
	 */
	@RequestMapping(value="selectList")
	public List<GoodsBrand> selectList(){
		List<GoodsBrand> goodsBrandList = this.goodsBrandService.selectList(null);
		
		logger.info("查找所有的商品品牌成功！");
		return goodsBrandList;
	}
	
	/**
	 * 根据条件查找商品品牌列表数据
	 * @param request
	 * @param currPage
	 * @param pageSize
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="/selectListByPage")
	@ResponseBody
	public PageView selectListByPage(HttpServletRequest request,String pageNow, String pageSize, String column, String sort){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		try{
			//查询商品数据
			ParamMap paramMap = getFormMap(ParamMap.class);
			paramMap = toFormMap(paramMap, pageNow, pageSize,paramMap.getStr("orderby"));
			paramMap.put("column", column);
			paramMap.put("sort", sort);
			
			List<GoodsBrand> goodsBrandList = this.goodsBrandService.selectList(paramMap);
			pageView.setRecords(goodsBrandList);
			
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			logger.info("查询商品品牌数据成功!");
		}catch(Exception e){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("查询商品品牌数据异常:"+e);
			throw new ScBizException("查询商品品牌数据异常！", e);
		}
		
		return pageView;
	}
	
	/**
	 * 跳转到编辑商品品牌的页面
	 * @param request
	 * @param goodsBrandId
	 * @return String
	 */
	@RequestMapping(value="addOrEdit")
	public String addOrEdit(HttpServletRequest request, Integer goodsBrandId){
		if(goodsBrandId!=null){
			GoodsBrand goodsBrand = this.goodsBrandService.selectByPrimaryKey(goodsBrandId);
			
			request.setAttribute("goodsBrand", goodsBrand);
		}
		
		return "goods/goodsBrand/edit";
	}
	
	/**
	 * 保存商品品牌
	 * @param request
	 * @param goodsBrand
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="saveGoodsBrand", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saveGoodsBrand(HttpServletRequest request, GoodsBrand goodsBrand){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		try{
			String dateTime = DateUtil.formatYMDHMSToString(new Date());
			
			Integer id = goodsBrand.getId();	//获取商品id
			int resultInt = 0;
			if(id==null){
				goodsBrand.setCreater("零售主管");
				goodsBrand.setCreateTime(dateTime);
				goodsBrand.setModifier("零售主管");
				goodsBrand.setModifyTime(dateTime);
				goodsBrand.setIsDelete("0");
				resultInt = this.goodsBrandService.insert(goodsBrand);
			}else{
				goodsBrand.setModifier("零售主管");
				goodsBrand.setModifyTime(dateTime);
				resultInt = this.goodsBrandService.update(goodsBrand);
			}
			
			if(resultInt>0){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
				logger.info("商品品牌数据保存成功！");
				return resultMap;
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SAVE);
				logger.warn("商品品牌数据保存出错！");
				return resultMap;
			}
			
		}catch(Exception e){
			logger.error("商品品牌数据保存异常！"+e);
			throw new ScBizException("商品品牌数据保存异常！", e);
		}
		
	} 
	
	/**
	 * 删除商品品牌
	 * @param ids
	 * @return String
	 */
	@RequestMapping(value="deleteGoodsBrand", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> deleteGoodsBrand(String ids){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		try{
			UserFormMap userFormMap = (UserFormMap)session.getAttribute("userSession");
			String dateTime = DateUtil.formatYMDHMSToString(new Date());
			
			GoodsBrand goodsBrand = new GoodsBrand();
			goodsBrand.setIds(ids.split(","));
			goodsBrand.setModifier(userFormMap.getStr("userName"));
			goodsBrand.setModifyTime(dateTime);
			goodsBrand.setIsDelete("1");
			
			int resultInt = goodsBrandService.update(goodsBrand);
			
			if(resultInt>0){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
				logger.info("商品品牌删除成功！");
				return resultMap;
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_DELETE);
				logger.warn("商品品牌删除失败！");
				return resultMap;
			}
		}catch(Exception e){
			logger.error("商品品牌数据删除异常！"+e);
			throw new ScBizException("商品品牌数据删除异常！", e);
		}
		
	}
	
}
